﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace VDPlayerDotNet
{
    /// <summary>
    /// VDPlayer_WPF.xaml 的交互逻辑
    /// </summary>
    public partial class VDPlayer_WPF : Window
    {
        private VDPlayer player;
        private bool isDrag = false;

        private bool autoPlay = true;
        public VDPlayer_WPF()
        {
            InitializeComponent();
            player = new VDPlayer();

            player.OnFrameCb += OnFrame;
            //player.SetOption(VDOptionType.OT_OpenModel, VDModule.OPEN_MODEL_CAMERA);
            player.SetOption(VDOptionType.OT_OutputPixFmt, VDModule.PIX_FMT_BGR24);
            player.SetOption(VDOptionType.OT_Loop, 1);
            player.SetOption(VDOptionType.OT_AutoPlay, autoPlay ? 1 : 0);
            //player.SetCameraRect(1920, 1080);
            textBox.Text = "F:/HTTPServer/体面.mp4";

        }

  

        private void OnFrame(_Frame frame)
        {

            //long start_t = VDModule.GetTimestamp();

            Bitmap bitmap = new Bitmap(frame.width, frame.height, frame.linesize[0], System.Drawing.Imaging.PixelFormat.Format24bppRgb, frame.data[0]);
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, ImageFormat.Bmp);
            ms.Seek(0, SeekOrigin.Begin);

            BitmapImage bitmapImg = new BitmapImage();
            bitmapImg.BeginInit();
            bitmapImg.StreamSource = ms;
            bitmapImg.EndInit();
            bitmapImg.Freeze();


            Dispatcher.Invoke(() =>
            {
                if(!isDrag)
                {
                    float rate = player.Duration == 0 ? 0 : (float)player.Progress / player.Duration;
                    slider.Value = rate * 1000;
                }
               
                if (player == null || player.Closed)
                    return;
                canvas.Source = bitmapImg;
            });
        }

        private void SelectBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择文件";
            //zip文件|*.zip|rar文件|*.rar|
            openFileDialog.Filter = "所有文件|*.*";
            openFileDialog.FileName = string.Empty;
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.DefaultExt = "zip";
            DialogResult result = openFileDialog.ShowDialog();
            if (result != System.Windows.Forms.DialogResult.OK)
                return;
            string fileName = openFileDialog.FileName;
            textBox.Text = fileName;
        }

        private void OpenBtn_Click(object sender, RoutedEventArgs e)
        {
            //player.Open("Logitech BRIO", OpenCB);
            player.Open(textBox.Text, OpenCB);

            if (autoPlay) pauseBtn.Content = "Pause";
            else pauseBtn.Content = "Play";
        }

        private void OpenCB(_DemuxInfo info)
        {
            if (info.retcode == 0)
                player.StartDemux(true, true);
            else
                Console.WriteLine("打开文件/设备失败!");
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            player.Close();
            pauseBtn.Content = "Pause";
        }

        private void PauseBtn_Click(object sender, RoutedEventArgs e)
        {

            if (player.IsPlaying)
            {
                pauseBtn.Content = "Play";
                player.Pause();
            }
            else
            {
                pauseBtn.Content = "Pause";
                player.Play();
                
            }
        }

        private void ScreenShotBtn_Click(object sender, RoutedEventArgs e)
        {
            int width = 0;
            int height = 0;
            IntPtr rgba = IntPtr.Zero;
            if (!player.ScreenShot(ref width, ref height, ref rgba)) return;

            VDModule.ReleaseMemory(rgba);
        }

        private void CloseWPFWindow(object sender, EventArgs e)
        {
            player.Release();
        }


        private void OnDragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            isDrag = false;
            double d = slider.Value / slider.Maximum;
            player.Seek(d);
        }

        private void OnDragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {
            isDrag = true;
        }

        private void OnMouseUpSlider(object sender, MouseButtonEventArgs e)
        {
            OnDragCompleted(null, null);
        }

        private void OnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                isDrag = true;
        }
    }
}
